home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d18 / turbotut.arc / MANUAL.EXE / lha / CHAP7.TXT < prev    next >
Text File  |  1988-11-27  |  6KB  |  124 lines

  1.                 CHAPTER 7 - Strings and string procedures
  2.  
  3.  
  4.                               PASCAL STRINGS
  5.  
  6.              According to the Pascal definition,  a string is simply
  7.         an  array  of  2 of more characters of  type  CHAR,  and  is
  8.         contained  in  an array defined in a VAR  declaration  as  a
  9.         fixed length.  Look at the example program STRARRAY.  Notice
  10.         that  the  strings are defined in the TYPE declaration  even
  11.         though  they could have been defined in the VAR part of  the
  12.         declaration. This is to begin getting you used to seeing the
  13.         TYPE declaration.  The strings defined here are nothing more
  14.         than arrays with CHAR type variables.
  15.  
  16.             The interesting part is the program.   Notice that  when
  17.         the  variable  "first_name" is assigned a value,  the  value
  18.         assigned  to  it must contain exactly 10 characters  or  the
  19.         compiler  will generate an error.   Try editing out a  blank
  20.         and you will get an invalid type error.   Pascal is neat  in
  21.         allowing  you  to write out the values in the  string  array
  22.         without specifically writing each character in a loop as can
  23.         be  seen in the "WRITELN" statement.   To combine the  data,
  24.         called  concatenation,   requires  the  use  of  the  rather
  25.         extensive  looping and subscripting seen in the last part of
  26.         the  program.   It  would  be even messier  if  we  were  to
  27.         consider  variable length fields which is nearly always  the
  28.         case in a real program.
  29.  
  30.             Two  things should be noticed in this  program.   First,
  31.         notice  the fact that the string operations are truly  array
  32.         operations  and  will  follow  all  of  the  characteristics
  33.         discussed in the last chapter.  Secondly, it is very obvious
  34.         that  Pascal is rather weak when it comes to its handling of
  35.         text type data.   Keep in mind that Pascal will handle  text
  36.         data,  even  though it may be difficult.   This concerns the
  37.         standard description of Pascal,  we will see next that TURBO
  38.         Pascal really shines here.
  39.  
  40.                        THE TURBO PASCAL STRING TYPE
  41.  
  42.             Look  at the example program STRINGS.   You will  see  a
  43.         much  neater program that actually does more.   TURBO Pascal
  44.         has,  as an extension to standard Pascal, the STRING type of
  45.         variable.  It is used as shown, and the number in the square
  46.         brackets in the VAR declaration is the maximum length of the
  47.         string.   In actual use in the program,  the variable can be
  48.         used  as any length from zero characters up to  the  maximum
  49.         given  in the declaration.   The variable "first_name",  for
  50.         example, actually has 11 locations stored for its data.  The
  51.         current length is stored in "first_name[0]" and the data  is
  52.         stored  in  "first_name[1]" through  "first_name[10]".   All
  53.         data is stored as byte variables,  including the  size,  and
  54.  
  55.  
  56.  
  57.                                  Page 33
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                 CHAPTER 7 - Strings and string procedures
  68.  
  69.  
  70.         the  length  is  therefore  limited  to  a  maximum  of  255
  71.         characters.
  72.  
  73.             Now  look  at  the  program  itself.   Even  though  the
  74.         variable  "first_name" is defined as 10 characters long,  it
  75.         is perfectly legal to assign it a 4 character constant, with
  76.         "first_name[0]"  automatically set to four and the last  six
  77.         characters undefined and unneeded.   When the program is run
  78.         the  three variables are printed out all  squeezed  together
  79.         indicating  that the variables are indeed shorter than their
  80.         full  size  as defined in the VAR  declaration.   Using  the
  81.         STRING  type  is  even easier when  you  desire  to  combine
  82.         several  fields into one as can be seen in the assignment to
  83.         "full_name".   Notice that there are even two blanks, in the
  84.         form  of  constant fields,  inserted between  the  component
  85.         parts of the full name.   When it is written out,  the  full
  86.         name is formatted neatly and is easy to read.
  87.  
  88.                     WHAT IS IN A STRING TYPE VARIABLE?
  89.  
  90.             The next example program named WHATSTRG,  is intended to
  91.         show you exactly what is in a string variable.  This program
  92.         is  identical  to  the last program except  for  some  added
  93.         statements  at the end.   Notice the assignment to  "total".
  94.         The  function "length" is available in TURBO Pascal to  find
  95.         out  what is the current length of any STRING type variable,
  96.         it  returns a byte type variable with the value of  the  [0]
  97.         position  of  the  variable.   We print out  the  number  of
  98.         characters  in the string at this point,  and then print out
  99.         each  character on a line by itself to illustrate  that  the
  100.         TURBO  Pascal  STRING  type  variable  is  simply  an  array
  101.         variable.
  102.  
  103.             The TURBO Pascal reference manual has a full description
  104.         of  several more procedures and functions available in TURBO
  105.         Pascal   only.    Refer  to  your  manual  for  a   complete
  106.         description given in chapter 9,  beginning on page 67.   The
  107.         use  of these should be clear after you grasp  the  material
  108.         covered here.
  109.  
  110.                            PROGRAMMING EXERCISES
  111.  
  112.         1.  Write a program in which you store your  first,  middle,
  113.             and last names as variables,  then display them one to a
  114.             line. Concatenate the names with blanks between them and
  115.             display your full name as a single variable.
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.                                  Page 34
  124.